package com.kuaimacode.kframework.admin.config.freemaker;

import com.kuaimacode.kframework.util.DictUtil;
import com.kuaimacode.kframework.util.StringUtil;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;

import java.util.HashMap;
import java.util.List;

/**
* Created by lwj on 2019-03-19.
*/
public class DictTemplate implements TemplateMethodModelEx {

    /**
     *
     * @param lable 字典标识
     * @return
     * @throws TemplateModelException
     */
    @Override
    public Object exec(List arguments) {
        if (null != arguments && 1 == arguments.size()) {
            String lable = String.valueOf(arguments.get(0));
            if(lable == null){
                return null;
            }else{
                return DictUtil.keyValueList(lable);
            }
        } else if (null != arguments && 2 == arguments.size()) {
            String lable = String.valueOf(arguments.get(0));
            String pCode = String.valueOf(arguments.get(1));
            if(lable == null|| pCode == null || "".equals(pCode)){
                return new HashMap<>();
            }else{
                return DictUtil.keyValueByPCodeList(lable,pCode);
            }
        }else {
            return null;
        }
    }
}
